<?php

namespace App\System\Cis\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Cis\Model\CisAdmin;
use App\System\Cis\Model\CisModule;
use App\System\Cis\Model\CisUser;
use App\System\Log\Model\LogAdmin;
use App\System\Log\Repositories\LogAdminRepository;


class CisAdminRepository extends Repository
{
    use SinglePattern;

    /*
     * 管理员列表
     *@$param $page 页数
     * @$param $limit 条数
     * @param $keyword 搜索关键词
     * @param $position 职务ID
     * @param $status 帐号状态
     * @return Collection
     * @auth Hjj
     */

    public function getlist($keyword, $position, $status, $page = 1, $limit = 10)
    {
        return CisAdmin::with(['role'])
            ->when($keyword, function ($query) use ($keyword) {
                return $query->where('admin_name', 'like', '%' . $keyword . '%');
            })
            ->when($position, function ($query) use ($position) {
                return $query->where('role_id', $position);
            })
            ->when($status, function ($query) use ($status) {
                return $query->where('status', $status);
            })
            ->paginate($limit, array('*'), 'page', $page);
    }

    /*
     * 新增管理员
     * @param array $data 要保存的数据
     * @author Hjj
     */
    public function addData($data)
    {
        $data['password'] = bcrypt($data['password']);
        $res = CisAdmin::create($data);
        if ($res) {
            $msg = '操作成功！';
        } else {
            $msg = '操作失败！';
        }
        return ['data' => $res, 'message' => $msg];
    }

    /*
     * 编辑管理员
     * @param $id 管理员ID
     * @param array $updata 修改的数据
     * @author Hjj
     */
    public function editData($id, $updata)
    {
        if (!$id) {
            return false;
        }
        $admin = CisAdmin::find($id);
        if (isset($updata['password'])) {
            $admin->password = bcrypt($updata['password']);
        }
        if (isset($updata['admin_name'])) {
            $admin->admin_name = $updata['admin_name'];
        }
        if (isset($updata['role_id'])) {
            $admin->role_id = $updata['role_id'];
        }
        if (isset($updata['status'])) {
            $admin->status = $updata['status'];
        }
        $bool = $admin->save();
        if ($bool) {
            $msg = '操作成功！';
        } else {
            $msg = '操作失败！';
        }
        return ['data' => $bool, 'message' => $msg];
    }

    /*
     * 获取管理员详情
     * @param $id 职务ID
     * @author Hjj
     */
    public function getDetail($id)
    {
        return CisAdmin::where('id', $id)->first();
    }

    /*
     * 删除管理员
     * @param $id 管理员ID
     * @author Hjj
     */
    public function delData($id)
    {
        if (!$id) {
            return false;
        }
        $bool = CisAdmin::where('id', $id)->first()->delete();
        if ($bool) {
            $msg = '操作成功！';
        } else {
            $msg = '操作失败！';
        }
        return ['data' => $bool, 'message' => $msg];
    }

    /*
     * 登录
     * @param $name 登录帐号
     * @param $pwd 密码
     * @author Hjj
     */
    public function login($name, $pwd)
    {
        $userInfo = CisAdmin::where('admin_name', '=', $name)
            ->first();
        if (empty($userInfo)) {
            return array('message' => '用户不存在', 'data' => '');
        }
        $password = $userInfo->password;
        if (\Hash::check($pwd, $password)) {//密码是否正确
            if ($userInfo->status == 1) {
                return ['message' => '帐号已被禁用', 'data' => ''];
            }
            \Session::put('name', $name);
            \Session::put('adminid', $userInfo->id);
            $logData['type'] = 0;
            $logData['log_info'] = '管理员'.$name.'登录';
            $logAdmin = LogAdminRepository::getInstance();
            $logAdmin->saveAdminLog($logData);
            return array('message' => '登录成功', 'data' => $userInfo->id, 'status' => 'SUCCESS');
        } else {
            return array('message' => '密码不正确', 'data' => '');
        }
    }

    /*
     * 管理员修改密码
     * @param $id 管理员id
     * @param $updata 更新数据
     * @author Hjj
     */
    public function changePwd($id, $updata)
    {
        if (!$id) {
            return false;
        }
        $userInfo = CisAdmin::where('id', $id)->first();
        if (\Hash::check($updata['oldPwd'], $userInfo->password)) {
            $admin = CisAdmin::find($id);
            $admin->password = bcrypt($updata['pwd']);
            $bool = $admin->save();
            if ($bool) {
                $msg = '操作成功！';
            } else {
                $msg = '操作失败！';
            }
            return ['data' => $bool, 'message' => $msg];
        } else {
            return ['message' => '旧密码输入不正确', 'data' => ''];
        }
    }

    /*
     * 获取管理员权限
     * @param $id 管理员id
     * @author Hjj
     */
    public function getAuth($id)
    {
        if (!$id) {
            return false;
        }
        $info = CisAdmin::with(['role'])->where('id', $id)->first();
        if ($info->role->action_list != 'all') {
            $moduleArr = explode(',', $info->role->action_list);
            $list = CisModule::whereIn('id', $moduleArr)->get();
            $arr = [];
            if (!$list->isEmpty()) {
                foreach ($list as $k => $v) {
//                    if($k == 0){
//                        $arr['loginUrl'] = $v->action_code;
//                    }
//                    $arr[$v->id]['name'] = $v->name;
//                    $arr[$v->id]['action_code'] = $v->action_code;
                    $arr[] = $v->action_code;
                }
            }
        } else {
            $arr = 'all';
        }
        \Session::put('authModule', $arr);
        return $arr;
    }


}